// schoolclass/mycustomer_setting/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    list:[],
    is_group:0,
    user_id:0,
    add_value:'',
    open:0,
    group_id:0,
    group_ids:0,
    id:0,
    title_num:99999,
    remarks_value:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
    console.log('e',e)
    that.data.group_id = e.group_id
    that.data.user_id = e.user_id
    that.get_list()
  },
  //点击完成
  complete:function(){
    var that = this
    if (that.data.remarks_value == "" && that.data.title_num == 99999) {
      wx.showToast({
        title: '请选择分组或输入备注',
        icon:'none'
      })
    } else {
      if (that.data.title_num == 99999) {
          getApp().request({
                  url: getApp().api.schoolclass.user_group_save,
                  method: "POST",
                  data: {
                        user_id:that.data.user_id,
                        group_id:that.data.group_id,
                        remarks:that.data.remarks_value
                  },   
                  success: function (res) {
                     console.log(res, '数据')
                     wx.showToast({
                       title: res.msg,
                       icon:'none',
                       success(res){
                        setTimeout(function(){ 
                          wx.navigateTo({
                            url:'/schoolclass/mycustomer/index'
                          })
                        }, 1500);
                       }
                     })
                  }
                });
      } else {
        getApp().request({
                url: getApp().api.schoolclass.user_group_save,
                method: "POST",
                data: {
                      user_id:that.data.user_id,
                      group_id:that.data.id,
                      remarks:that.data.remarks_value
                },   
                success: function (res) {
                   console.log(res, '数据')
                   wx.showToast({
                     title: res.msg,
                     icon:'none',
                     success(res){
                      setTimeout(function(){ 
                        wx.navigateTo({
                          url:'/schoolclass/mycustomer/index'
                        })
                      }, 1500);
                     }
                   })
                }
              });
      }
    }
  },
  //备注内容
  remarks:function(e){
    console.log(e,'eeeeeeeeeeeeeeeeeee')
    var that = this
    that.setData({
      remarks_value:e.detail.value
    })
  },
  //点击选中分组
  get_fenzu:function(e){
    var that = this
    that.setData({
      title_num : e.currentTarget.dataset.index,
      id : e.currentTarget.dataset.id
    })
  },
  //编辑弹窗中点击确定
  get_queding:function(){
    var that = this
    getApp().request({
            url: getApp().api.schoolclass.edit_user_group,
            method: "POST",
            data: {
                  user_id:that.data.user_id,
                  group_id:that.data.group_ids,
                  group_name:that.data.add_value
            },   
            success: function (res) {
               console.log(res, '数据')
               wx.showToast({
                 title: res.msg,
                 icon:'none'
               })
               that.get_list()
               that.setData({
                showModal: false
              })
            }
          });
  },
  //获取到编辑室文本框的值
  group_value:function(e){
    console.log(e,'eee')
    var that = this
    that.data.add_value = e.detail.value
  },
  //点击打开分组关闭分组
  select:function(){
    var that= this
    if(that.data.open == 0){
      that.setData({
        open:1
      })
    }else{
      that.setData({
        open:0
      })
    }
  },
  //编辑分组名称弹窗
  edit:function(e){
    this.setData({
      showModal: true,
      group_ids:e.currentTarget.dataset.id
      })
  },//取消显示
  close_mask: function () {
      this.setData({
        showModal: false
      })
    },
  //删除分组
  del:function(e) {
    console.log(e,'eeeeeee')
    var that = this
    getApp().request({
            url: getApp().api.schoolclass.user_group_del,
            method: "POST",
            data: {
                  group_id:e.currentTarget.dataset.id
            },   
            success: function (res) {
               console.log(res, '数据')
               wx.showToast({
                 title: res.msg,
                 icon:'none'
               })
               that.get_list()
            }
          });
  },
  //分组列表
  get_list() {
    var that = this
    that.setData({
      list:[]
    })
    getApp().request({
            url: getApp().api.schoolclass.user_group_list,
            method: "GET",
            data: {
                  page:that.data.page,
                  user_id:that.data.user_id
            },   
            success: function (res) {
               console.log(res, '数据')
              for(var i = 0;i<res.data.user_group_list.length;i++){
                that.data.list.push(res.data.user_group_list[i])
                that.setData({
                  list:that.data.list
                })
              }
            }
          });
  },
  //获取到添加分组文本框的值
  add_value:function(e){
    console.log(e,'获取到添加分组文本框的值')
    var that = this
    that.data.add_value = e.detail.value
  },
  //添加分组
  add:function(e) {
    var that = this
    console.log('获取到添加分组文本框的值',that.data.add_value)
    if(that.data.add_value == ''){
      wx.showToast({
        title: '请输入分组名称',
        icon:'none'
      })
    }else{
      getApp().request({
              url: getApp().api.schoolclass.edit_user_group,
              method: "POST",
              data: {
                    user_id:that.data.user_id,
                    group_id:0,
                    group_name:that.data.add_value
              },   
              success: function (res) {
                 console.log(res, '数据')
                  wx.showToast({
                    title: res.msg,
                    icon:'none'
                  })
                  that.get_list();
              }
            });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.setData({
      page:that.data.page + 1
    })
   that.get_list()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})